<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reports extends Backend_Controller {

	protected $_title = 'Ujian / Ulangan';
	
	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="teacher")
			redirect('login/index/1');
		$this->load->model('school_years_model', 'school_years', true);
		$this->load->model('tests_model', 'tests', true);
		$this->load->model('users_model', 'users', true);
	}
		
	public function index($offset=0, $sort_by='name') {
		
		$data = array(
			'mainview' => 'index',
		) + $this->_get_header_data();
		
		$this->load->view('teacher/template',$data);
		
	}
	
	function _crud_output($output = null) {
        $this->load->view('teacher/template_crud.php',$output);    
    }

	function reports_display(){	
		$crud = new grocery_CRUD();
        $crud->set_model('reports_crudmodel');
		$crud->set_theme('flexigrid');
        $crud->set_table('classes');
		
		$crud->columns('name','room_code');
		$crud->display_as('name','Kelas yang Diajar');
		$crud->display_as('room_code','Jenis Ulangan');
		$crud->callback_column('room_code',array($this,'_callback_room_code'));
		$crud->unset_add();
		$crud->unset_edit();
		$crud->unset_delete();

        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	function _callback_room_code($value, $row){
		$rs = $this->school_years->get_active_school_year();
		$school_year_id = $rs[0]['id'];
		$rs = $this->tests->get_tests_info($school_year_id, $row->id);
		$retval = '- ';
		$search = array("<p>","</p>");

		foreach($rs as $rs_test){
			$rs_test['information'] = str_replace($search,"",$rs_test['information']);
			$retval .= anchor('teacher/reports/report_class_display/'. $rs_test['id'] . '/' . $row->id,$rs_test['information']);
			$retval .= ' - ';
		}
		return $retval;
	}
	
	function report_class_display($test_id=0, $class_id=0){	
		$crud = new grocery_CRUD();
        $crud->set_model('test_question_student_crudmodel');
		$crud->set_theme('flexigrid');
        $crud->set_table('students');
		
		$crud->columns('test_id','name','class_id');
		$crud->display_as('test_id','No');
		$crud->display_as('name','Nama');
		$crud->display_as('class_id','Nilai');
		
		$_SESSION['report_number'] = 1;
		$_SESSION['report_test_id'] = $test_id;
		$_SESSION['report_class_id'] = $class_id;
		
		$crud->callback_column('test_id',array($this,'_callback_test_id'));
		$crud->callback_column('name',array($this,'_callback_name'));
		$crud->callback_column('class_id',array($this,'_callback_class_id'));
		$crud->unset_add();
		$crud->unset_edit();
		$crud->unset_delete();

        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	function _callback_test_id($value, $row){
		$retval = $_SESSION['report_number'];
		$_SESSION['report_number']++;		
		return $retval;
	}
	
	function _callback_name($value, $row){
		$name = anchor('teacher/reports/report_student_display/' . $_SESSION['report_test_id'] . '/' . $row->nis, $value);
		return $name;
	}
	
	function _callback_class_id($value, $row){
		$mark = $this->tests->get_marks($_SESSION['report_test_id'], $row->nis);
		if($mark=='')
			$mark = '0';
		else if ($mark<75)
			$mark = '<span id=mark style="background-color:red; color:white"> ' . $mark . '</span>';
		return $mark;
	}
	
	
	function report_student_display($test_id, $nis){	
		$crud = new grocery_CRUD();
		
        $crud->set_model('test_question_student_detail_crudmodel');
		$crud->set_theme('flexigrid');
        $crud->set_table('test_question_student');
		
		$crud->columns('number_in_test', 'problem', 'option_a', 'option_b', 'option_c', 'option_d', 'option_e', 'student_answer','judge');
		$crud->display_as('number_in_test','No');
		$crud->display_as('problem','Soal');
		$crud->display_as('option_a','Pilihan A');
		$crud->display_as('option_b','Pilihan B');
		$crud->display_as('option_c','Pilihan C');
		$crud->display_as('option_d','Pilihan D');
		$crud->display_as('option_e','Pilihan E');
		$crud->display_as('student_answer','Jawaban Siswa');
		$crud->display_as('judge','Hasil Koreksi');
		
		$crud->callback_column('judge',array($this,'_callback_judge'));
		$crud->callback_column('problem',array($this,'_callback_problem'));
		$crud->unset_add();
		$crud->unset_edit();
		$crud->unset_delete();
		
		$_SESSION['nis'] = $nis;
		$_SESSION['test_id'] = $test_id;
		
        $output = $crud->render();
		$output->nis = $nis;
		
		$student = $this->users->get_student($nis);
		$rs = $this->school_years->get_active_school_year();
		$school_year_id = $rs[0]['id'];
		$rs = $this->tests->get_tests_info($school_year_id, $student['class_id'], $test_id);
		
		$output->name = $student['student_name'];
		$output->test_name = $rs[0]['information'];
        $this->_crud_output($output);	
	}
	
	function _callback_problem($value, $row){
		$retval = "<div style='width:620px; height:200px; overflow-x:scroll'>" .  $value . "</div>";
		return $retval;
	}
	
	function _callback_judge($value, $row){
		if($value == 0 && $row->student_answer != '')
			$value = '<div id=value style="width:inherit; text-align:center; background-color:red; color:white"> salah </div>';
		else if ($value<75 && $row->student_answer != '')
			$value = '<div id=mark style="width:inherit; text-align:center; background-color:green; color:white"> BENAR </div>';
		return $value;
	}
}
